﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CSharpExample
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 连接相机
        /// </summary>
        /// <param name="configPath">配置文件路径</param>
        /// <returns>连接成功返回0，否则连接错误</returns>
        [DllImport("CameraController.dll", EntryPoint = "Connect")]
        public static extern int ConnectCamera(string configPath);

        /// <summary>
        /// 拍取点云文件
        /// </summary>
        /// <param name="pointCloudFile">输出的点云文件</param>
        /// <returns>拍照成功返回0，否则拍照错误</returns>
        [DllImport("CameraController.dll", EntryPoint = "CapturePointCloud")]
        public static extern int CapturePointCloud(string pointCloudFile);

        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <returns></returns>
        [DllImport("CameraController.dll", EntryPoint = "Close")]
        public static extern void CloseCamera();

        private readonly Dictionary<int, string> kErrorCodeDict = new Dictionary<int, string>();

        public MainForm()
        {
            InitializeComponent();
            kErrorCodeDict.Add(0, "成功");
            kErrorCodeDict.Add(1, "配置文件不存在");
            kErrorCodeDict.Add(2, "配置文件无效");
            kErrorCodeDict.Add(3, "日志初始化失败");
            kErrorCodeDict.Add(4, "相机参数设置失败");
            kErrorCodeDict.Add(5, "相机连接失败");
            kErrorCodeDict.Add(6, "点云范围设置失败");
            kErrorCodeDict.Add(7, "相机增益设置失败");
            kErrorCodeDict.Add(8, "相机未连接");
            kErrorCodeDict.Add(9, "相机扫描失败");
            kErrorCodeDict.Add(10, "打光图像生成失败");
        }

        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenCamera(object sender, EventArgs e)
        {
            int ret = ConnectCamera("config.json");
            if (ret != 0)
            {
                MessageBox.Show(kErrorCodeDict[ret], "相机连接错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                btnCloseCamera.Enabled = true;
                btnCapture.Enabled = true;
                btnOpenCamera.Enabled = false;
            }
        }

        /// <summary>
        /// 拍取点云数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Caputre(object sender, EventArgs e)
        {
            int ret = CapturePointCloud("temp");
            if (ret != 0)
            {
                MessageBox.Show(kErrorCodeDict[ret], "相机拍照错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseCamera(object sender, EventArgs e)
        {
            CloseCamera();
            btnOpenCamera.Enabled = true;
            btnCapture.Enabled = false;
            btnCloseCamera.Enabled = false;
        }
    }
}
